#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/*
 * force type conversion of struct.
 */

typedef struct aaa
{
	unsigned char a1;
	unsigned char a2;
	char a3[6];
	char a4[8];
}aaa;

struct tt 
{
	char a;
	void *private_data;
};

struct wd
{
	char a;
	int b;
	int c;
	struct tt t;
};

int test(struct tt *ts)
{
//	struct tt *t;
	struct wd *w;
	char *p;
	
	ts->a = 'a';
	ts->private_data = "wiaozhi_1234567890";
	
	//w = (struct wd *)t->private_data;
	w = ts->private_data;
	p = ts->private_data;
	
	//							wiao   zhi_ 1234  567890
	//							w      z    1     5      wiaozhi_1234567890
	printf("%c %c %c %c %s\n", w->a, w->b, w->c, w->t.a, p);
	
	return 0;
}

int (*pf)(void);

int f(void)
{
	printf("================= wise\n");
	return 1;
}

void test_pf()
{
	pf = &f;
	pf();
	pf = ****f;
	pf();
	(***************************************************f)();
}
int re()
{
	return ((int []){1,2,3,4})[3];
}

unsigned long foo()
{
	return (unsigned long) - 1 / 8;		// 536870911
}

unsigned long foo2()
{
	return ((unsigned long) - 1) / 8;	// 536870911
}

unsigned long foo3()
{
	return (unsigned long) (-1 / 8);	// 0
}

/**********************************************************************/
typedef struct 
{
	char *key;
	char *value;
}T1;

typedef struct
{
	long type;
	char *value;
}T3;

T1 a[] = {
			{
				"",
				((char *)&((T3) {1, (char *) 1}))
			}
		};
		
int T()
{
	T3 *pt3 = (T3*)a[0].value;
	int value;
	char *b = "xiaozhi";
	char c[] = {'a', 'b', 'c', 'd', 'e', 'f'};
	
	value = sizeof(a);
	printf("T() : %d %d %d %d sizeof c=%d strlen c=%d\n", 
					 value, sizeof(long),
					 sizeof(b), strlen(b), 
					 sizeof(c), strlen(c));
	return (int)pt3->value;
}
/********************************************************************/

int xxx = 7;
int fxxx()
{
	int xxx = 3;
	{
		extern int xxx;
		return xxx;
	}
}

int iii()
{
	return (unsigned long)((1 - sizeof(int)) >> 31);
}

struct 
{
	int x;
	struct 
	{
		int y, z;
	}nested;
} ix = { .nested.y = 5, 6, .x = 1, 2};

void print_ix()
{
	printf("ix.nested.y = %d, ix.nested.x = %d, ix.x = %d\n", ix.nested.y, ix.nested.z, 0);
}

void invoke()
{
	printf("invoke re   ======== %d\n",  re());
	printf("invoke foo  ======== %ld\n", foo());
	printf("invoke foo2 ======== %ld\n", foo2());
	printf("invoke foo3 ======== %ld\n", foo3());
	printf("invoke T    ======== %d\n", T());
	printf("invoke iii    ======== %d\n", iii());
	printf("invoke fxxx    ======== %d\n", fxxx());
	print_ix();
}

int saaa()
{
	struct tt t;
	struct wd w;
	int value;
	
	test(&t);
	test_pf();
	(***************************test_pf)();
	invoke();
	
	return 0;
}

typedef struct
{
	char data[16];
	short checksum;
}dataset;

typedef struct
{
	short flag;
	dataset data[6];
}frame;

int sbbb(void)
{
	frame ft;
	unsigned char data[100];
	int i, j, k;
	char *ptr;
	
	frame *pfr;
	
	printf("struct size %d\n", sizeof(pfr));
	
	for (i = 0; i < 100; i++) {
		data[i] = i + 1;
	}
	
	k = 0;
	for (i = 0; i < 6; i++) {
		for (j = 0; j < 16; j++) {
			ft.data[i].data[j] = data[k];
			k++;
		}
	}
	
	ptr = (char *)&ft;
	for (i = 0; i < 100; i++)
		printf("%5d", *ptr++);
		
	putchar(0xd);
		
	return 0;
}

struct ec
{
	int num;
	int count;
	char something[20];
}stu[3] = {{12, 13, "esp"}, {15,16, "sk"}, {17, 18, "we"}};

int sccc()
{
	struct ec *p;
	
	p = stu;
	//p = (struct ec *) &stu[0].num;
	//printf("%d\n", p);
	//printf("%d\n", p + 1);
	
	p = (struct ec *) &stu[0];
	printf("%d\t%d\t%s\n", p->num, p->count, p->something);
	printf("%d\t%d\t%s\n", (p+1)->num, (p+1)->count, (p+1)->something);
	
	return 0;
}

typedef struct A {
        int a;
        char b;
}A;

A tmp = {10, '0'};

void print(void)
{
        printf("tmp.a = %d tmp.b = %c\n", tmp.a, tmp.b);

        printf("&tmp = %p &tmp.a = %p &tmp.b = %p\n", 
                        (unsigned long)(void *)&tmp, 
                        (unsigned long)(void *)&tmp.a,
                        (unsigned long)(void *)&tmp.b);

        printf("sizeof tmp = %d sizeof tmp.a = %d sizeof tmp.b = %d\n", 
                        sizeof(A),
                        sizeof(int),
                        sizeof(char));
}

void priBit(int t)
{
        int i = 32;

        printf("t = %d\n", t);
        for (; i > 0; i--)
        {
                printf("%d", (t >> i) & 0x0001);
        }
        printf("\n");
}

int _main()
{
        int *p = NULL;

        print();
        p = (int *)&tmp;
        priBit(*p);
        priBit(*(p + 1));
}

struct stu
{
        char name[20];
        int num;
};

int m_ain()
{
        void *p = NULL;
        struct stu a={"li main",5};
        p=&a;
        printf(" %s %d  \n",( (struct stu *) p )->name,( (struct stu *) p )->num);

		_main();

		return 0;
}

int main()
{
	char buf[30];
	
	memset(buf, '0', sizeof(buf));
	strcpy(buf,"ABCDEFGHIJKLMNOPQRSTUVWXYZ");
	
	printf("buf length : %d\n", sizeof(buf));	// 30
	printf("struct length : %d\n", sizeof(struct aaa));	// 16
	
	aaa *aa;
	aa = (struct aaa*)buf;
	
	printf("a1 is %d\n", aa->a1);
	printf("a2 is %d\n", aa->a2);
	printf("a3 is %s\n", aa->a3);		// why not overflow, xiaozhi?
	printf("a4 is %s\n", aa->a4);		// ditto
	printf("a3 is %.6s\n", aa->a3);
	printf("a4 is %.8s\n", aa->a4);

	saaa();
	
	return 0;
}



